package com.oyjd.driver;

import java.io.File;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

import com.oyjd.FileOP;
import com.oyjd.HttpNet;
import com.oyjd.RegexUtil;

public class Down2 {
	// public static String outDirverPath = "J:/temp/";
	// public static String outSqlPath = "J:/temp/";
	public static String outDirverPath = "/sl/driver/";
	public static String outSqlPath = "/sl/down/";

	private static String base = "http://www.bkill.com/download/";
	private static String url1 = "list-135-{0}.html";
	public static String reg_list = "<dt><a href=\"(.*?)\" target=\"_blank\" title=";

	public static String reg_title = "<h1 class=\"title_h1\">(.*?)</h1>";
	public static String reg_plat = "<li class=\"pingtai\">应用平台：<span>(.*?)</span></li>";
	public static String reg_down = "<li><a href=\"(.*?)\" target=\"_blank\">";

	public static String sql = "insert into T_ADB_DRIVER (DRIVER_NAME,BUSINESS_ID,DEVICE_ID,FIT_BIT,OP_SYS,NOTE,SOU_PATH,ID,CREATOR,CREATE_TIME,LAST_EDITOR,LAST_UPDATE_TIME,STATUS) values (''{0}'',''{1}'',''{2}'',{3},{4},''{5}'',''{6}'',''{7}'',''admin'',now(),''admin'',now(),0);";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		new File(outSqlPath + "driverSql2.txt").delete();
		for (int i = 1; i <= 21; i++) {
			System.out.println("---------->page:" + i);
			for (String s : getList(i)) {
				try {
					String c = getDetail(base + s);
					doOne(c);
				} catch (Exception err) {
					err.printStackTrace();
				}
			}
		}

	}

	public static List<String> getList(int page) throws Exception {
		String u = MessageFormat.format(base + url1, page);
		byte[] b = new HttpNet().getBytesFromNet(u);
		String c = new String(b, "gb2312");
		return RegexUtil.getMuti(c, reg_list);
	}

	public static String getDetail(String url) throws Exception {
		byte[] b = new HttpNet().getBytesFromNet(url);
		return new String(b, "gb2312");
	}

	public static void doOne(String c) throws Exception {
		String title = RegexUtil.getSingle(c, reg_title);
		String plat = RegexUtil.getSingle(c, reg_plat);
		List<String> tmpList = RegexUtil.getMuti(c, reg_down);
		String downName = downOne(tmpList);
		String busiId = Dconst.getId(title);
		if (null == busiId) {
			return;
		}
		List<Integer> ops = Dconst.getOp(plat);
		for (int op : ops) {
			String fsql = MessageFormat.format(sql, title, busiId, "", 32, op, title, downName, FileOP.getUUID());
			FileOP.writeFile(outSqlPath + "driverSql2.txt", fsql + "\r\n", "gb2312", true);
		}
		System.out.println("--->" + title + "---" + plat + "---" + downName);
	}

	/**
	 * 下载驱动文件
	 * 
	 * @param tmpList
	 * @return
	 * @throws Exception
	 */
	public static String downOne(List<String> tmpList) throws Exception {
		String flag = null;
		HttpNet hn = new HttpNet();
		Collections.sort(tmpList);
		String name = "";
		for (int i = 0; i < tmpList.size(); i++) {
			String s = tmpList.get(i);
			if (0 == i) {
				name = s.substring(s.lastIndexOf("id=") + 3).replaceAll("::", "") + ".zip";
			}
			String outName = outDirverPath + name;
			if (!new File(outName).exists()) {
				if (hn.downFile(s, null, outName)) {
					flag = name;
					break;
				} else {
					new File(outName).delete();
				}
			} else {
				flag = name;
				break;
			}

		}
		return flag;
	}
}
